<template>
  <div class="app-container">
    <el-button type="primary" @click="warningSetting">告警接收人设置</el-button>
    <el-table v-loading="loading" :data="studentList">
      <el-table-column label="设备编码" align="center" prop="deviceId" />
      <el-table-column label="异常状态" align="center" prop="errorStatus">
        <template slot-scope="scope">
          {{ scope.row.errorStatus == 1 ? '在线' : '离线' }}
        </template>
      </el-table-column>
      <el-table-column label="使用者" align="center" prop="user">
        {{ '小一班' }}
      </el-table-column>
      <el-table-column label="异常时间" align="center" prop="abnormalTime">
        {{ '2024-02-01' }}
      </el-table-column>
      <el-table-column label="异常持续时间" align="center" prop="anomalyDuration">
        {{ '5个小时' }}
      </el-table-column>
      <el-table-column label="报警参数" align="center" prop="alarmParameter">
        {{ '设备故障报警' }}
      </el-table-column>
      <el-table-column label="设备名称" align="center" prop="deviceName" />
      <el-table-column label="设备类型" align="center" prop="deviceType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.deviceType_type" :value="scope.row.deviceType" />
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <el-dialog title="告警接收人设置" :visible.sync="dialogVisible" width="45%" @close="handleClose">
      <span style="font-weight:600 ;font-size:16px ;">告警接收人设置：</span>
      <el-select v-model="value1" multiple placeholder="请选择告警接收人" @change="changeBtn" style="width:500px ;">
        <el-option v-for="item in options" :key="item.name" :label="item.name" :value="item.id">
        </el-option>
      </el-select>
      <div style="margin-top:20px ;">
        <div>
          <p style="font-weight:600 ;font-size:16px ;">告警接收人列表：</p>
          <el-table v-loading="loadingList" :data="showOption">
            <el-table-column label="序号" align="center" type="index"/>
            <el-table-column label="姓名" align="center" prop="name" />
            <el-table-column label="手机号" align="center" prop="phone" />
            </el-table>
        </div>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false" size="small">取 消</el-button>
        <el-button type="primary" @click="addBtn" size="small">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getControlError, selectTeacherPhone, addTeacherPhone, getTeacherPhone } from "@/api/base/deviceControl";

export default {
  name: "Student",
  dicts: ['deviceType_type', 'devicestatus_status', 'devicestatus_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,

      // 学生表格数据
      studentList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      value1: [],
      options: [],
      dialogVisible: false,
      arr: [],
      showOption:[],
      schoolCode:this.$store.getters.schoolCode,
      loadingList:false
    };
  },
  created() {
    this.getList();
    this.selectTeacherPhone()
  },
  methods: {
    /** 查询学生列表 */
    getList() {
      this.loading = true;
      getControlError(this.queryParams).then(response => {
        this.studentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    warningSetting() {
      this.dialogVisible = true
      this.getTeacherPhone()

    },
    async selectTeacherPhone() {
      const { data, code, msg } = await selectTeacherPhone()
      if (code == 200) {
        this.options = data
      } else {
        this.$message.error(msg)
      }
    },
    async getTeacherPhone() {
      this.loadingList = true
      const { data, code, msg } = await getTeacherPhone()
      if (code == 200) {
      this.loadingList = false
        this.showOption = data
        const val = []
        data.forEach(it=>{
          val.push(it.id)
        })
        this.value1 = val
      } else {
        this.$message.error(msg)
        this.loadingList = false
      }
    },
    async addBtn() {
      const { code, msg } = await addTeacherPhone(this.arr)
      if(code == 200){
        this.$message.success('设置成功')
        this.dialogVisible = false
      }else{
        this.$message.error(msg)
      }
    },
    changeBtn(val) {
      let newArr = []
      this.options.forEach(item => {
        val.forEach(it => {
          if (item.id == it) {
            newArr.push(item)
          }
        })
      })
      this.arr = newArr
    },
    handleClose(){
      this.arr = []
      this.value1 = []
    }
  }
};
</script>
